package com.menu.Service.Impl;

import com.menu.Mapper.MenuMapper;
import com.menu.Mapper.StepMapper;
import com.menu.Mapper.UserMapper;
import com.menu.Pojo.Menu;
import com.menu.Pojo.Step;
import com.menu.Pojo.User;
import com.menu.Service.StepService;
import com.menu.Utils.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.util.ArrayList;

@Service
@Slf4j
public class StepServiceImpl implements StepService {
    @Resource
    MenuMapper menuMapper;
    @Resource
    StepMapper stepMapper;
    @Resource
    UserMapper userMapper;

    private final static  String UPLOAD_STEP = "/Static/user/step/";
    @Override
    public ResponseResult deleteFile(Integer userId, Integer stepId) {
        User user= userMapper.findByUserId(userId);
        Step step= stepMapper.findByStepId(stepId);
        if(step==null){
            return ResponseResult.error("空");
        }
            String realPath=System.getProperty("user.dir")+UPLOAD_STEP+stepId;
            log.info(realPath);
            File file=new File(realPath);
            File[] files=  file.listFiles();
            if(file.exists()&&file.isFile()){
                Boolean q= file.delete();
                return ResponseResult.success("删除成功",q);
            }else if(files!=null){
                for(File file1:files){
                    Boolean q=file1.delete();
                    return ResponseResult.success("删除成功",q);
                }
            }
        return ResponseResult.error("文件不存在");
    }
}
